﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Web;
using System.Web.Mvc;
using Isha.Data.Infrastructure;
using Isha.Entities;
using Isha.Infrastructure;
using Isha.Repositories;
using Isha.Repositories.Infrastructure;
using Isha.Services.Infrastructure;

namespace Isha.Services
{
    internal class ContentItemService : ServiceBase, IContentItemService
    {
        bool autherized = true;

        public ContentItemService()
        {
            Initialize(null, null);
        }

        public ContentItemService(IContentItemRepository contentitemRepository, IUnitOfWork unitOfWork)
        {
            Initialize(contentitemRepository, unitOfWork);
        }

        public IContentItemRepository ContentItemRepository { get; private set; }

        public IUnitOfWork UnitOfWork { get; private set; }

        public void Initialize(IContentItemRepository contentitemRepository, IUnitOfWork unitOfWork)
        {
            autherized = HttpContext.Current.Request.IsAuthenticated;
            ContentItemRepository = (contentitemRepository ?? DependencyResolver.Current.GetService<IContentItemRepository>());
            UnitOfWork = (unitOfWork ?? DependencyResolver.Current.GetService<IUnitOfWork>());
        }

        public IEnumerable<ContentItem> FetchChildItems(long Id)
        {
            var contentitems = ContentItemRepository.GetChildContentItems(Id);
            return contentitems;
        }

        public ContentItem FetchContentItemByLink(string linktext)
        {
            var contentitem = ContentItemRepository.Find(p => p.Link == linktext && p.ExcludeNavigation == false
                && ((p.IsPublished == true || (p.IsPublished == false && p.PublishEndDate >= DateTime.Now)) || autherized == true));
            return contentitem;
        }

        public ContentItem FetchContentItemByLink(string linktext, int parentItemId)
        {
            var contentitem = ContentItemRepository.Find(p => p.Link == linktext
                                && p.ParentItemID == parentItemId && p.ExcludeNavigation == false
                                && ((p.IsPublished == true || (p.IsPublished == false && p.PublishEndDate >= DateTime.Now)) || autherized == true));
            return contentitem;
        }

        public ContentItem FetchHome()
        {
            int HomeItemID = IshaConfig.GetConfiguration().Home;

            var contentitem = ContentItemRepository.Find(p => p.ContentItemID == HomeItemID &&
                ((p.IsPublished == true || (p.IsPublished == false && p.PublishEndDate >= DateTime.Now)) || autherized == true));

            return contentitem;
        }

        public ContentItem FetchContentItem(long Id)
        {
            var contentitem = ContentItemRepository.Find(p => p.ContentItemID == Id && p.ExcludeNavigation == false
                && ((p.IsPublished == true || (p.IsPublished == false && p.PublishEndDate >= DateTime.Now)) || autherized == true));
            return contentitem;
        }

        public IEnumerable<ContentItem> FetchContentItems()
        {
            var contentitems = ContentItemRepository.GetAll();
            return contentitems;
        }

        public void CreateContentItem(ContentItem contentitem)
        {
            contentitem.UrlFix();
            ContentItemRepository.Add(contentitem);
            UnitOfWork.Commit();
        }

        public void UpdateContentItem(ContentItem contentitem)
        {
            contentitem.UrlFix();
            ContentItemRepository.Update(contentitem);
            UnitOfWork.Commit();
        }

        public void DeleteContentItem(long id)
        {
            ContentItemRepository.Delete(id);
            UnitOfWork.Commit();
        }

        public IEnumerable<ContentItem> FetchContentItemsByParent(long Id)
        {
            var contentitems = ContentItemRepository.GetMany(p => p.ParentItemID == Id && p.ExcludeNavigation == false
                && ((p.IsPublished == true || (p.IsPublished == false && p.PublishEndDate >= DateTime.Now)) || autherized == true)).OrderBy(p => p.DisplayOrder);
            return contentitems;
        }

        public ContentItem Fetch404()
        {
            var contentitem = ContentItemRepository.Find(p => p.Link == "404");
            return contentitem;
        }

    }
}
